﻿using System;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace CEFdemo
{
    public class Client : IDisposable
    {
        public TcpClient client;
        public NetworkStream stream;
        private bool disposed = false;

        public Client()
        {
            client = new TcpClient("127.0.0.1", 8080);
            stream = client.GetStream();
        }

        public async Task<string> SendMessageAsync(string message)
        {
            try
            {
                byte[] data = Encoding.UTF8.GetBytes(message);

                // 发送信息
                await stream.WriteAsync(data, 0, data.Length);

                // 接收来自服务器的响应
                byte[] responseData = new byte[1024];
                int bytesRead = await stream.ReadAsync(responseData, 0, responseData.Length);
                string responseMessage = Encoding.UTF8.GetString(responseData, 0, bytesRead);
                return responseMessage;
            }
            catch (Exception ex)
            {
                // 处理通信异常
                Console.WriteLine($"Error during communication: {ex.Message}");
                throw;
            }
        }
        
        public async Task<string> SendFileAsync(byte[] filecontent)
        {
            try
            {
                await stream.WriteAsync(filecontent, 0, filecontent.Length);
                return "File part sent";
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error during communication: {ex.Message}");
                throw;
            }
        }
        
        public async Task<string> RecevieResponse()
        {
            try
            {
                byte[] responseData = new byte[1024];
                int bytesRead = await stream.ReadAsync(responseData, 0, responseData.Length);
                string responseMessage = Encoding.UTF8.GetString(responseData, 0, bytesRead);
                return responseMessage;
            }
            catch (Exception ex)
            {
                // 处理通信异常
                Console.WriteLine($"Error during communication: {ex.Message}");
                throw;
            }
        }
        

        //实现IDisposable接口
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    // 释放托管资源
                    if (stream != null)
                    {
                        stream.Close();
                        stream = null;
                    }
                    if (client != null)
                    {
                        client.Close();
                        client = null;
                    }
                }
        
                // 释放非托管资源（如果有）
        
                disposed = true;
            }
        }
        
        // 析构函数（终结器）
        ~Client()
        {
            Dispose(false);
        }
    }
}
